package com.sdkj.dspservice.modules.doc.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdkj.dspservice.common.base.entity.DataEntity;
import com.sdkj.dspservice.common.utils.StringUtils;
import lombok.Data;

import javax.validation.constraints.NotBlank;

/**
 * 文件Entity
 *
 * @author tm
 * @version 2020/11/20 10:44
 */
@Data
@TableName(value = "doc_doc")
public class Doc extends DataEntity<Doc> {

    /**
     * 名称
     */
    @NotBlank(message = "未传入简介")
    private String name;
    /**
     * 标签
     */
    private String topic;
    /**
     * 简介
     */
    private String description;
    /**
     * 浏览次数
     */
    private Integer browseNumber;
    /**
     * 下载次数
     */
    private Integer downloadNumber;
    /**
     * 点赞次数
     */
    private Integer goodNumber;
    /**
     * 是否置顶
     */
    private String isTop;
    /**
     * 类型（1：报表文件  2：埋点文档）
     */
    private String type;
    /**
     * 附件ids
     */
    @NotBlank(message = "未上传文件")
    private String annexIds;

    /**
     * 用于查询
     */
    @TableField(exist = false)
    private String searchValue;

    public Doc() {
    }

    public Doc(String id) {
        super(id);
    }

    public void addBrowseNumber(){
        if(browseNumber == null){
            browseNumber = 0;
        }
        browseNumber = browseNumber + 1;
    }

    public void addDownloadNumber(){
        if(downloadNumber == null){
            downloadNumber = 0;
        }
        downloadNumber = downloadNumber + 1;
    }

    public void addGoodNumber(){
        if(goodNumber == null){
            goodNumber = 0;
        }
        goodNumber = goodNumber + 1;
    }

    @Override
    public QueryWrapper<Doc> makeQueryWrapper(QueryWrapper<Doc> queryWrapper) {
        if(StringUtils.isNotBlank(type)){
            queryWrapper.eq("a.type", type);
        }
        if(StringUtils.isNotBlank(name)){
            queryWrapper.nested(qw-> qw.like("a.name", name).or().like("a.description", name));
        }
        queryWrapper.orderByDesc("a.is_top");
        return super.makeQueryWrapper(queryWrapper);
    }
}
